package com.gitee.linzl.zuul.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gitee.linzl.zuul.gateway.ZuulPropertiesExt;
import com.gitee.linzl.zuul.gateway.fiters.DBRefreshableRouteLocator;
import com.gitee.linzl.zuul.gateway.fiters.pre.PreDecorationFilterExt;
import com.gitee.linzl.zuul.gateway.mapper.ZuulRouteExtMapper;

/**
 * 参考 ZuulProxyAutoConfiguration 修改
 * 
 * @description
 * @author linzl
 * @email 2225010489@qq.com
 * @date 2018年10月3日
 */
@Configuration
@EnableConfigurationProperties({ ZuulPropertiesExt.class })
public class ZuulProxyAutoConfigurationExt {

	@Autowired
	protected ZuulProperties zuulProperties;
	@Autowired
	protected ZuulPropertiesExt zuulPropertiesExt;

	@Autowired
	protected ServerProperties server;

	@Autowired
	private ZuulRouteExtMapper mapper;

	@Bean
	public PreDecorationFilterExt preDecorationFilter(RouteLocator routeLocator,
			ProxyRequestHelper proxyRequestHelper) {
		return new PreDecorationFilterExt(routeLocator, this.server.getServlet().getContextPath(),
				this.zuulProperties, proxyRequestHelper);
	}

	@Bean(name = "SimpleRouteLocator")
	public SimpleRouteLocator simpleRouteLocator() {
		return new DBRefreshableRouteLocator(this.server.getServlet().getContextPath(), this.zuulProperties,
				this.mapper);
	}

}
